import { Project } from '@entity/project'
import { JoinProjectRequest } from '@entity/join_project'
import { Component, Vue } from 'vue-property-decorator'
import project_axios from '@axios/project';
import ws_axios from '@axios/websocket'
import Tag from '@tag';
import { User } from '@/assets/js/entity/user';

@Component({
    components: {
        Tag
    }
})
export class ProjectInfo extends Vue {
    
    /** 项目 */
    private project: Project = this.$store.getters.project(this.id);

    /** 被选中 tag 的下标 */
    private index: number = -1;

    /** 给发布人的留言 */
    private msg: string = '';

    /** 职位的称呼 */
    private tag: string = '';

    private get id(): number {
        return Number.parseInt(this.$route.params.id);
    }

    /**
     * 根据路由中的 id 从本地读取项目数据 
     */
    created() {
        project_axios.base.project(this.id).then((res: Project) => {
            this.project = res;
        });
    }

    /**
     * 选择的下标
     */
    private select(index: number): void {
        this.index = index;
        this.tag = this.project.positions[index].tag;
    }

    /**
     * 是否选择了某个下标
     */
    private isSelect(index: Number): Boolean {
        return this.index == index;
    }

    /**
     * 发送加入申请
     */
    private join(): void {
        if (this.index == -1) {
            '请点击选择你要担任的职务'.notifyError();
            return;
        }
        if (this.msg.isEmpty()) {
            '请添加留言'.notifyError();
            return;
        }
        let data: JoinProjectRequest = new JoinProjectRequest(this.project.id, this.project.name, this.tag, this.msg);
        ws_axios.join.request(this.project.master, data);
    }

    /**
     * 当自己不是项目发布人时返回true
     */
    get notMaster(): boolean {
        let me: User = this.$store.getters.me();
        return me != undefined && this.project.master.id != me.id;
    }
}